package com.iwunu.gateway.http;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.util.Map;

/**
 * @author kin207
 * @date 2020-07-27 14:27
 * @desc
 */
public class HttpClientUtil {
	public static JSONObject doGet(String url, Map<String, String> param) {

		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();

		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();
			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);
			// 执行请求
			response = httpclient.execute(httpGet);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return JSONObject.parseObject(resultString);
	}

	public static JSONObject doGet(String url) {
		System.out.println(url);
		return doGet(url, null);
	}

	public static JSONObject doPost(String url, JSONObject param) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			StringEntity se = new StringEntity(param.toString(), "utf-8");
			se.setContentType("text/json");
			se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
			httpPost.setEntity(se);
			httpPost.setEntity(se);
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return JSONObject.parseObject(resultString);
	}

	public static JSONObject doPost(String url) {
		return doPost(url, null);
	}

	public static JSONObject doPostJson(String url, JSONObject param, String token) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			if (StringUtils.isNotEmpty(token)) {
				httpPost.addHeader("X-SDX-Token", token);
				httpPost.addHeader("Content-Type", "application/json");
			}
			StringEntity se = new StringEntity(param.toJSONString());
			se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
			httpPost.setEntity(se);
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return JSONObject.parseObject(resultString);
	}

	public static JSONObject doPostJson(String url, String param, String token) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			if (StringUtils.isNotEmpty(token)) {
				httpPost.addHeader("X-SDX-Token", token);
				httpPost.addHeader("Content-Type", "application/json");
			}
			StringEntity se = new StringEntity(param);
			se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
			httpPost.setEntity(se);
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return JSONObject.parseObject(resultString);
	}

	public static JSONObject doPutJson(String url, JSONObject param, String token) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPut httpPut = new HttpPut(url);
			if (StringUtils.isNotEmpty(token)) {
				httpPut.addHeader("X-SDX-Token", token);
				httpPut.addHeader("Content-Type", "application/json");
			}
			StringEntity se = new StringEntity(param.toString());
			se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
			httpPut.setEntity(se);
			response = httpClient.execute(httpPut);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return JSONObject.parseObject(resultString);
	}

	public static JSONObject loginPost(String url, Map<String, String> param) {

		try {
			PostMethod postMethod = new PostMethod(url); // api为访问的url
			postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			int size = param.size();
			int index = 0;
			NameValuePair[] body = new NameValuePair[size];
			for (Map.Entry<String, String> bodyItem : param.entrySet()) {
				body[index++] = new NameValuePair(bodyItem.getKey(), bodyItem.getValue());
			}
			postMethod.setRequestBody(body);

			HttpClient httpClient = new HttpClient();
			int response = httpClient.executeMethod(postMethod); // 执行POST方法 正常返回200
			String result = postMethod.getResponseBodyAsString();

			return JSONObject.parseObject(result);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}
